<style>
#ShiftingPermissionAddPage .tree .selected {
    background-color: #D6E4F6;
}
#ShiftingPermissionAddPage span.error {
    left: 260px;
    position: absolute; 
}
</style>
<div class="pageContent" id="ShiftingPermissionAddPage">
    <div style="width: 220px; float: left; border: 1px solid #D8DFE5;">
        <div class="mini-toolbar">
            <input type="text" placeholder="搜索工作中心" style="width: 110px;" id="searchWorkCenter" /> 
            <a class="buttonActive" href="javascript:void(0)" id="searchTree">
                <span><@label key='search.B'>搜索</@label></span>
            </a>
        </div>
        <div layoutH="72">
            <#if workCentersTree??>
            <ul id="workcenterTree" class="tree expand">
                <#list workCentersTree as workCenter>
                <li><a code="${workCenter.workCenterCode!''}" lockFlag="${workCenter.lockFlag == 1}" >${workCenter.workCenterText!''}</a> 
                    <#if workCenter.childrens??>
                    <ul>
                        <@macroWorkCenterTree children=workCenter.childrens />
                    </ul> 
                    </#if>
                </li> 
                </#list>
            </ul>
            </#if>
        </div>
        <!-- 递归产生树的宏定义 -->
        <#macro macroWorkCenterTree children> 
            <#if children?? && children?size gt 0> 
                <#list children as workCenter>
                <li><a code="${workCenter.workCenterCode!''}" lockFlag="${workCenter.lockFlag == 1}" >${workCenter.workCenterText!''}</a> 
                    <#if workCenter.childrens??>
                    <ul>
                        <@macroWorkCenterTree children=workCenter.childrens />
                    </ul> 
                    </#if>
                </li>
                </#list> 
            </#if> 
        </#macro>
    </div>
    <div style="margin-left: 222px; padding: 0px; margin-top: -3px; overflow: hidden;">
        <form class="pageForm">
            <div class="pageFormContent">
                <fieldset>
                    <dl class="nowrap">
                        <dt><@label key="userCode.C,colon">用户编号：</@label></dt>
                        <dd>
                            <input name="userCode" size="22" type="text" maxlength="20" class="required  alphanumeric toUp"
                            id="addShiftingPermission.userCode"
                            postField="userCode" suggestFields="userCode,userText" 
                            suggestUrl="${rc.getContextPath()}/manage/user/userSuggest" lookupGroup="addShiftingPermission"
                            remote="${rc.getContextPath()}/manage/user/checkedUserCodeNotExits"
                            warn="<@label key='userCode.C,doesNotExist.C'>用户编号不存在</@label>" />
                        </dd>
                    </dl>
                    <dl class="nowrap">
                        <dt><@label key="userText.C,colon">用户名称：</@label></dt>
                        <dd>
                            <input size="22" class="required" name="userText" id="addShiftingPermission.userText" type="text" maxlength="60" readonly="readonly" />
                        </dd>
                    </dl>
                </fieldset>
            </div>
            <div class="formBar">
                <ul style="float: left;">
                    <li>
                        <div class="buttonActive">
                            <div class="buttonContent">
                                <button title="输入用户后，可双击工作中心快速添加" id="addShiftingPermissionToGrid" type="button">添加</button>
                            </div>
                        </div>
                    </li>
                    <li>
                        <div class="buttonActiveGrey">
                            <div class="buttonContent">
                                <button id="deleteShiftingPermissionFromGrid" type="button" >移除</button>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
        </form>
        <div id="jGridForShiftingPermissionAdd" style="float: left; width: 100%;"></div>
    </div>
</div>
<div class="formBar">
    <ul>
        <li>
            <div class="buttonActive">
                <div class="buttonContent">
                    <button id="saveShiftingPermissions" type="button"><@label key="save.B">保存</@label></button>
                </div>
            </div>
        </li>
        <li>
            <div class="buttonActiveGrey">
                <div class="buttonContent">
                    <button id="cancelShiftingPermissions" type="button" ><@label key="cancel.B">取消</@label></button>
                </div>
            </div>
        </li>
    </ul>
</div>
<script type="text/javascript">
(function($){
    var $page = DWZ.context();
    var $grid = $('#jGridForShiftingPermissionAdd', $page).jGrid({
        height: 263,
        width: '100%',
        base: '${rc.getContextPath()}/manage/shiftingPermission/',
        autoLoad: false,
        orderable: false,
        hasBorder: false,
        pageable: false,
        columns: [
            {field: 'userCode;workcenterCode', group: 'ids', type: 'checkbox', width: 22},
            {field: 'userCode', text: '<@label key="userCode.C">用户编号</@label>', width: 60},
            {field: 'userText', text: '<@label key="userText.C">用户名称</@label>', width: 60},
            {field: 'workcenterCode', text: '<@label key="workCenterCode.C">工作中心编号</@label>', width: 80},
            {field: 'workcenterText', text: '<@label key="workCenterText.C">工作中心描述</@label>', width: 100}
        ]
    });
    
    var checkExistPermission = function (data) {
        var check = 'true';
        $.ajax({
            url: '${rc.getContextPath()}/manage/shiftingPermission/unexistPermissionByWorkcenterAndUser',
            type: 'POST',
            data: data,
            global: false,
            cache: false,
            async: false,
            dataType: 'text',
            success: function (text) {
                check = text;
            }
        });
        if ($('[type=checkbox][value="' + data.userCode + ';' + data.workcenterCode + '"]', $grid).length > 0) {
            check = 'exist';
        }
        return check;
    }
    $('#addShiftingPermissionToGrid', $page).on('click', function(){
        var $this = $(this), $form = $this.parents('form:first'), $workcenter = $('#workcenterTree li>div.selected>a', $page), 
        existWorkcenter = true, existUsername = true, data = {};
        if ($workcenter.length == 0) {
            existWorkcenter = false;
        } else {
            data.workcenterCode = $workcenter.attr('code');
            data.workcenterText = $workcenter.text();
            data.lockFlag = $workcenter.attr('lockFlag');
        }
        if (!$form.valid()) {
            existUsername = false;
        } else {
            data.userCode = $('[name=userCode]', $form).val();
            data.userText = $('[name=userText]', $form).val();
        }
        if (!existWorkcenter && !existUsername) {
            alertMsg.error('请选择工作中心并指定用户！');
        } else if (!existWorkcenter) {
            alertMsg.error('请选择工作中心！');
        } else if (!existUsername) {
            alertMsg.error('请为工作中心指定用户！');
        }
        if (!existWorkcenter || !existUsername) {
            return false;
        }
        var check = checkExistPermission(data);
        if (check == 'true') {
            if (data.lockFlag == 'true') {
                /*
                alertMsg.confirm('所选工作中心已被锁定，是否继续添加？', {okCall: function() {
                    $grid.jGrid('appendRow', data);
                }});
                */
                alertMsg.error('所选工作中心已被锁定，不能添加权限！');
            } else {
                $grid.jGrid('appendRow', data);
            }
        } else if (check == 'exist') {
            alertMsg.error('该工作中心已经添加该用户权限！');
        } else {
            alertMsg.error('该用户已经拥有该工作中心权限！');
        }
    });
    
    $('#deleteShiftingPermissionFromGrid', $page).on('click', function(){
        var existChecked = false;
        $.each($('tbody [type=checkbox]', $grid), function(){
            if (this.checked) {
                existChecked = true;
                $(this).parents('tr:first').remove();
                $grid.jGrid('refresh');
            }
        });
        if (!existChecked) {
            alertMsg.error(DWZ.msg("alertSelectMsg"));
        }
    });
    
    var closeDialog = function() {
        $.pdialog.close($page);
    };
    
    $('#saveShiftingPermissions', $page).on('click', function(){
        if ($('tbody [type=checkbox]', $grid).length == 0) {
            alertMsg.error('当前列表中无可保存的数据！');
        } else {
            var permissions = new Array();
            $('tbody [type=checkbox]', $grid).each(function(){
                var values = $(this).val().split(';');
                permissions.push({
                    userCode: values[0],
                    workcenterCode: values[1]
                });
            });
            $.DWZajax({
                url: '${rc.getContextPath()}/manage/shiftingPermission/saveShiftingPermissions',
                data: {
                    navTabId: '${navTabId}',
                    shiftingPermissions: JSON.stringify(permissions)
                }
            });
            closeDialog();
        }
    });
    
    $('#cancelShiftingPermissions', $page).on('click', function(){
        if ($('tbody [type=checkbox]', $grid).length > 0) {
            alertMsg.confirm('当前列表中的数据没有保存至数据库，确认取消吗？', {okCall: closeDialog});
        } else {
            closeDialog();
        }
    });
    
    $("#searchTree", $page).baseTreeSearch({
        inputTextId : "#searchWorkCenter", //搜索框的id
        alertMsg : "<@label key='treeSearchContent.D'>请输入内容再查找</@label>", //提示内容，要考虑国际化，传自后台
        $p : $page
    });
    
    $('.tree', $page).on('selectstart', 'a', function(){
        return false;
    }).on('dblclick', 'a', function(){
        $('#addShiftingPermissionToGrid', $page).click();
    }).css({
        '-webkit-user-select': 'none',
        '-moz-user-select': 'none'
    });
})(jQuery);
</script>